##################################################################################################################################
#
# Copyright (C) 2008-2012 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_group {} {
	set values [$::daidze_main get_values]
# 	puts $values

	set msg_list [dict create name "Group name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db transaction start
		$::db insert_all daidze_user_level $values
		set query "ALTER TABLE daidze_access_real ADD COLUMN [dict get $values name] boolean DEFAULT True"
		$::db execute_query $query
		set query "CREATE OR REPLACE VIEW v_permissions_[dict get $values name] AS SELECT daidze_access_real.id, daidze_modules.name AS \"Module name\", daidze_modules_forms.name AS \"Form name\", daidze_forms_widgets.name AS \"Widget name\", daidze_access_real.[dict get $values name] FROM daidze_access_real, daidze_forms_widgets, daidze_modules, daidze_modules_forms WHERE daidze_access_real.fk_daidze_modules = daidze_modules.id AND daidze_access_real.fk_daidze_modules_forms = daidze_modules_forms.id AND daidze_access_real.fk_daidze_forms_widgets = daidze_forms_widgets.id"
		$::db execute_query $query
		$::db transaction stop
		$::daidze_main load_main daidze_groups groups.xml $::main 1
	}
}

proc delete_group {table_var} {
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set row [$my_tablelist cellcget [$my_tablelist curselection],0 -text]
	set group_name [$my_tablelist cellcget [$my_tablelist curselection],1 -text]
	if {$row == 1} {
		tk_messageBox -icon error -type ok -message "[::msgcat::mc "This is the first group in the system. You cant delete it."]"
	} else {
		$::db transaction start
		$::db delete_row daidze_user_level $row
		set query "DROP VIEW v_permissions_$group_name"
		$::db execute_query $query
		set query "ALTER TABLE daidze_access_real DROP COLUMN $group_name"
		$::db execute_query $query

		$::db transaction stop
		$::daidze_main load_main daidze_groups groups.xml $::main 1
	}

}



proc edit_group {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set user_var {id name def description}
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
	foreach key $user_var value $row {
		dict set sdata $key $value
	}

	$::daidze_main load_main daidze_groups edit_group.xml $::main
	$::daidze_main update_form $sdata	
}

proc update_group {} {
	set values [$::daidze_main get_values]
# 	puts $values

	set msg_list [dict create name "Group name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db transaction start
		set old_group_name [$::db select_name_by_id daidze_user_level [dict get $values id]]
		$::db update_all daidze_user_level $values
		set query "DROP VIEW v_permissions_$old_group_name"
		$::db execute_query $query
		set query "ALTER TABLE daidze_access_real DROP COLUMN $old_group_name"
		$::db execute_query $query
		set query "ALTER TABLE daidze_access_real ADD COLUMN [dict get $values name] boolean DEFAULT True"
		$::db execute_query $query
		set query "CREATE OR REPLACE VIEW v_permissions_[dict get $values name] AS SELECT daidze_access_real.id, daidze_modules.name AS \"Module name\", daidze_modules_forms.name AS \"Form name\", daidze_forms_widgets.name AS \"Widget name\", daidze_access_real.[dict get $values name] FROM daidze_access_real, daidze_forms_widgets, daidze_modules, daidze_modules_forms WHERE daidze_access_real.fk_daidze_modules = daidze_modules.id AND daidze_access_real.fk_daidze_modules_forms = daidze_modules_forms.id AND daidze_access_real.fk_daidze_forms_widgets = daidze_forms_widgets.id"
		$::db execute_query $query
		$::db transaction stop
		$::daidze_main load_main daidze_groups groups.xml $::main 1
	}
}